export default {
  props: {
    localstorageKey: {
      type: String,
      default: null
    }
  },
  computed: {},
  created() {
    this.scrollBarTop = 0;
  },
  data() {
    return {};
  },
  mounted() {
    this.initEvents();
  },
  beforeDestroy() {
    this.destroyEvents();
  },
  methods: {
    initEvents() {
      window.addEventListener('resize', this.resizeTable);
    },
    destroyEvents() {
      window.removeEventListener('resize', this.resizeTable);
    },
    resize() {
      this.initAfter();
      this.$refs.tableScrollX && this.$refs.tableScrollX.resetScroll();
      this.$refs.tableScroll && this.$refs.tableScroll.resetScroll();
    },
    resizeTable() {
      this.initAfter();
      this.scrollYHandel(0);
      this.scrollXHandel(0);
      this.$nextTick(() => {
        this.$refs.tableScroll && this.$refs.tableScroll.reset();
        this.$refs.tableScrollX && this.$refs.tableScrollX.reset();
        this.initAfterScrollTableBody();
      });
    },
    scrollYHandel(y) {
      let isDown = false;
      if (y > this.scrollBarTop) {
        isDown = true;
      }
      this.scrollBarTop = y;
      // 向下
      this.updataTableScroll(isDown);
    },
    setStartIndex() {
      let scrolly = 500;
      if (this.tableScroll) {
        scrolly = this.tableScroll.y;
      }
      let maxPageLine = Number(scrolly / this.calcLineHeight) + this.lineScrollBufferCount;
      this.cutDataSource = this.calcSortData.slice(this.startIndex, this.startIndex + maxPageLine);
    },
    updataTableScroll(isDown) {
      let { calcLineHeight, scrollBarTop, lineScrollBufferCount } = this;
      let lineScrollBufferCountHeight = lineScrollBufferCount * calcLineHeight;
      let diffScrollBarTop = scrollBarTop - this.startIndex * calcLineHeight;
      if (isDown) {
        if (diffScrollBarTop > lineScrollBufferCountHeight) {
          let count = parseInt(diffScrollBarTop / lineScrollBufferCountHeight);
          this.startIndex = this.startIndex + lineScrollBufferCount * count;
        }
      } else {
        let scrollBarTmp = this.scrollBarTop - this.startIndex * calcLineHeight;
        if (scrollBarTmp < 0) {
          let count = Math.ceil((scrollBarTmp * -1) / lineScrollBufferCountHeight);
          this.startIndex = this.startIndex - lineScrollBufferCount * count;
        }
      }
      this.scrollBarY = this.scrollBarTop - this.startIndex * calcLineHeight;
      if (this.mainTable) {
        this.table.map((table) => {
          table.style.transform = `translateY(${-this.scrollBarY}px)`;
        });
        this.mainTable.style.transform = `translate(${-this.scrollBarX}px, ${-this.scrollBarY}px)`;
      }
    },
    scrollXHandel(x) {
      this.scrollBarX = x;
      if (this.mainTable) {
        this.tableDom.tableHeaderTable &&
          (this.tableDom.tableHeaderTable.style.transform = `translateX(${-x}px)`);
        this.mainTable.style.transform = `translate(${-this.scrollBarX}px, ${-this.scrollBarY}px)`;
      }
    },
    mousewheelHandel(e) {
      if (!this.calcOpenVirtuallyModel) return;
      let e1 = e.originalEvent || window.event || e;
      let wheelDeltaY = -e1.deltaY || e1.wheelDeltaY;
      let wheelDeltaX = -e1.deltaX || e1.wheelDeltaX;

      let absYNum = Math.abs(wheelDeltaY);
      let absXNum = Math.abs(wheelDeltaX);
      if (absYNum > absXNum) {
        // 只滑动Y
        if (wheelDeltaY && this.$refs.tableScroll) {
          let scrollTop = this.scrollBarTop - wheelDeltaY;
          scrollTop < 0 && (scrollTop = 0);
          if (scrollTop > this.tableDataHeight - this.scrollHeight) {
            scrollTop = this.tableDataHeight - this.scrollHeight;
          }
          // 更新滑块
          this.$refs.tableScroll.setScrollbarOffset(scrollTop);
          this.scrollYHandel(scrollTop);
        }
      } else {
        // 只滑动X
        if (wheelDeltaX && this.$refs.tableScrollX) {
          let scrollBarX = this.scrollBarX - wheelDeltaX;
          scrollBarX < 0 && (scrollBarX = 0);
          if (scrollBarX > this.tableColumnsWidth - this.outerClientRect.width) {
            scrollBarX = this.tableColumnsWidth - this.outerClientRect.width;
          }
          this.$refs.tableScrollX.setScrollbarOffset(scrollBarX);
          this.scrollXHandel(scrollBarX);
        }
      }

      e.preventDefault();
    },
    loadDragObj() {
      if (this.localStorageObj) {
        return this.localStorageObj;
      }
      if (this.localstorageKey) {
        let localStr = localStorage.getItem(this.localstorageKey);
        if (localStr) {
          this.localStorageObj = JSON.parse(localStr);
        } else {
          this.localStorageObj = {};
        }
      } else {
        this.localStorageObj = null;
      }
      return this.localStorageObj;
    },
    loadFieldsOptionMenus() {
      if (this.localstorageKey) {
        let catchFieldsOptionMenus = this.fieldsOptionMenus;
        let localStr = localStorage.getItem('menu-' + this.localstorageKey);
        if (localStr) {
          let localMenus = JSON.parse(localStr);
          let localMap = {};
          let localMenuMap = {};
          // 开始检查是否匹配当前列,过滤不存在的列
          localMenus.filter((menu, index) => {
            if (this.initColumnsSortMap[menu.colKey] !== undefined) {
              localMap[menu.colKey] = index;
              localMenuMap[menu.colKey] = menu;
              return true;
            }
          });
          // 更新顺序
          catchFieldsOptionMenus = catchFieldsOptionMenus.sort((a, b) => {
            if (a.colKey && b.colKey) {
              return localMap[a.colKey] - localMap[b.colKey];
            } else {
              return 0;
            }
          });
          // 更新选中状态
          catchFieldsOptionMenus.forEach((menu) => {
            if (localMenuMap[menu.colKey] && localMenuMap[menu.colKey].value !== undefined) {
              menu.value = localMenuMap[menu.colKey].value;
            }
          });
          this.fieldsOptionMenus = catchFieldsOptionMenus;
          // 更新选中字符串
          this.catchRightDefaultMenusObj = catchFieldsOptionMenus
            .filter((el) => el.value)
            .map((el) => el.colKey)
            .join(',');
        }
      }
    },
    saveFieldsOptionMenus() {
      if (this.localstorageKey) {
        // 缩减属性
        let data = this.fieldsOptionMenus.map((el) => {
          return { colKey: el.colKey, dataIndex: el.dataIndex, value: el.value };
        });
        localStorage.setItem('menu-' + this.localstorageKey, JSON.stringify(data));
      }
    },
    saveDragObj() {
      if (this.localStorageObj) {
        localStorage.setItem(this.localstorageKey, JSON.stringify(this.localStorageObj));
      }
    },
    setDragWidth(colKey, width) {
      this.loadDragObj(); // 怕是异步的key；再次load一次
      if (this.localStorageObj) {
        this.localStorageObj[colKey] = width;
        this.saveDragObj();
      }
    },
    getDragWidth(colKey) {
      if (this.localStorageObj) {
        return this.localStorageObj[colKey];
      }
    },
    cleanDFieldsOptionMenus() {
      if (this.localstorageKey) {
        localStorage.removeItem('menu-' + this.localstorageKey);
      }
    },
    cleanDragWidth(colKey) {
      if (this.localStorageObj) {
        if (colKey) {
          delete this.localstorageKey[colKey];
          this.saveDragObj();
        } else {
          localStorage.removeItem(this.localstorageKey);
        }
      }
    }
  }
};
